package com.integralPerformance.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.integralPerformance.common.Result;
import com.integralPerformance.mapper.PointsCriteriaMapper;
import com.integralPerformance.pojo.entity.PointsCriteria;
import com.integralPerformance.pojo.form.add.AddPointsCriteria;
import com.integralPerformance.pojo.form.update.UpdatePointsCriteria;
import com.integralPerformance.pojo.query.PointsCriteriaQuery;
import com.integralPerformance.pojo.vo.BaseVo;
import com.integralPerformance.service.PointsCriteriaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author admin
 * @description 针对表【points_criteria(业绩评分标准信息)】的数据库操作Service实现
 * @createDate 2024-03-10 22:25:58
 */
@Service
public class PointsCriteriaServiceImpl extends ServiceImpl<PointsCriteriaMapper, PointsCriteria>
        implements PointsCriteriaService {

    @Autowired
    PointsCriteriaMapper pointsCriteriaMapper;

    @Override
    public Result<List<String>> getType() {
        return Result.success(pointsCriteriaMapper.getType());
    }

    @Override
    public Result<?> getPointsCriteria(PointsCriteriaQuery pointsCriteriaQuery) {
        Page<PointsCriteria> page = new Page<>(pointsCriteriaQuery.getCurrentPage(), pointsCriteriaQuery.getPageSize());
        Page<PointsCriteria> result = pointsCriteriaMapper.getPointsCriteria(page, pointsCriteriaQuery);

        BaseVo<PointsCriteria> criteriaBaseVo = new BaseVo<>();
        criteriaBaseVo.setCurrentPage(pointsCriteriaQuery.getCurrentPage());
        criteriaBaseVo.setPageSize(pointsCriteriaQuery.getPageSize());
        criteriaBaseVo.setAllPages(result.getPages());
        criteriaBaseVo.setAllRecordNum(result.getTotal());
        criteriaBaseVo.setRecords(result.getRecords());

        return Result.success(criteriaBaseVo);
    }

    @Override
    public Result<List<String>> getLevelByType(String type) {
        return Result.success(pointsCriteriaMapper.getLevelByType(type));
    }

    @Override
    public Result<List<String>> getCategory(String type, String level) {
        return Result.success(pointsCriteriaMapper.getCategory(type, level));
    }

    @Override
    public Result<List<String>> getGainLevel(String type, String level, String category) {
        return Result.success(pointsCriteriaMapper.getGainLevel(type, level, category));
    }

    @Override
    public Result<String> updatePointsCriteria(UpdatePointsCriteria updatePointsCriteria) {
        pointsCriteriaMapper.updateById(updatePointsCriteria);
        return Result.success("更新成功");
    }

    @Override
    public Result<String> addPointsCriteria(AddPointsCriteria addPointsCriteria) {
        PointsCriteria pointsCriteria = new PointsCriteria();
        BeanUtil.copyProperties(addPointsCriteria,pointsCriteria);
        pointsCriteriaMapper.insert(pointsCriteria);
        return Result.success("添加成功");
    }

    @Override
    public Result<String> deletePointsCriteriaById(String id) {
        pointsCriteriaMapper.deleteById(id);
        return Result.success("删除成功");
    }

    @Override
    public Result<List<Map<String, Object>>> getPointsCriteriaTree() {
        // 查询全部数据
        List<PointsCriteria> pointsCriteriaList = pointsCriteriaMapper.selectList(null);

        // 构建树状结构
        List<Map<String, Object>> result = buildTree(pointsCriteriaList);

        return Result.success(result);
    }

    private List<Map<String, Object>> buildTree(List<PointsCriteria> pointsCriteriaList) {
        // 使用流处理构建树状结构
        Map<String, Map<String, Map<String, List<String>>>> treeMap = new LinkedHashMap<>();

        // 遍历全部数据，构建树状结构
        pointsCriteriaList.forEach(criteria -> {
            String type = criteria.getPerformanceType();
            String level = criteria.getProjectLevel();
            String category = criteria.getProjectCategory();
            String gainLevel = criteria.getGainLevel();

            // 如果任一属性为null，则跳过当前节点
            if (type == null || level == null || category == null || gainLevel == null) {
                return;
            }

            // 添加节点到树状结构
            treeMap.computeIfAbsent(type, k -> new LinkedHashMap<>())
                    .computeIfAbsent(level, k -> new LinkedHashMap<>())
                    .computeIfAbsent(category, k -> new ArrayList<>())
                    .add(gainLevel);
        });

        // 构建结果列表
        List<Map<String, Object>> result = new ArrayList<>();
        treeMap.forEach((type, typeMap) -> {
            Map<String, Object> typeNode = new LinkedHashMap<>();
            typeNode.put("label", type);
            typeNode.put("value", type);
            typeNode.put("children", buildLevelNodes(typeMap));
            result.add(typeNode);
        });

        return result;
    }

    private List<Map<String, Object>> buildLevelNodes(Map<String, Map<String, List<String>>> levelMap) {
        List<Map<String, Object>> levelNodes = new ArrayList<>();
        levelMap.forEach((level, categoryMap) -> {
            Map<String, Object> levelNode = new LinkedHashMap<>();
            levelNode.put("label", level);
            levelNode.put("value", level);
            levelNode.put("children", buildCategoryNodes(categoryMap));
            levelNodes.add(levelNode);
        });
        return levelNodes;
    }

    private List<Map<String, Object>> buildCategoryNodes(Map<String, List<String>> categoryMap) {
        List<Map<String, Object>> categoryNodes = new ArrayList<>();
        categoryMap.forEach((category, gainLevelList) -> {
            Map<String, Object> categoryNode = new LinkedHashMap<>();
            categoryNode.put("label", category);
            categoryNode.put("value", category);
            categoryNode.put("children", buildGainLevelNodes(gainLevelList));
            categoryNodes.add(categoryNode);
        });
        return categoryNodes;
    }

    private List<Map<String, Object>> buildGainLevelNodes(List<String> gainLevelList) {
        List<Map<String, Object>> gainLevelNodes = new ArrayList<>();
        gainLevelList.forEach(gainLevel -> {
            Map<String, Object> gainLevelNode = new LinkedHashMap<>();
            gainLevelNode.put("label", gainLevel);
            gainLevelNode.put("value", gainLevel);
            gainLevelNodes.add(gainLevelNode);
        });
        return gainLevelNodes;
    }

}




